package cn.picclife.mwx.manager.base;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(value = { char[].class })
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class CharArrayTypeHandler extends BaseTypeHandler<char[]> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, char[] parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, String.valueOf(parameter));
    }

    @Override
    public char[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String reString = rs.getString(columnName);
        return StringUtils.trim(reString).toCharArray();
    }

    @Override
    public char[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String reString = rs.getString(columnIndex);
        return reString.toCharArray();
    }

    @Override
    public char[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String reString = cs.getString(columnIndex);
        return StringUtils.trim(reString).toCharArray();
    }

}
